<?php
namespace app\api\controller\deerhome\user;

use app\common\controller\Api;
use app\common\library\Sms;
use EasyWeChat\Factory;

use think\Db;
/**
 * 套餐次卡
 */
class Demo extends Api
{
    public $app = null;
    public function _initialize()
    {
        $config = [
            'app_id' => 'wx2c33e44bd0c7d647',
            'secret' => 'b6778d75b052ed5e01dfdaf9deab2034',
            'token' => 'gongxiduo',
        ];
        $this->app = Factory::officialAccount($config);
    }

    public function test(){
        $request_data = $this->request->param();
        if(empty($request_data['orderid']))exit('orderid 为空');
        $this->smsSend($request_data);
    }
    public function smsSend($request_data)
    {
        $this->qqq('wechat_template',$request_data);


        if(!empty($request_data['orderid'])){
            $info = db('deerhome_order')->where('id',$request_data['orderid'])->find();
        }

        $touserid = db('wechat')->column('openid');
        foreach ($touserid as $item){
            $item_requet_data = [
                'phrase7' => empty($info['item_name']) ? '工喜多家政服务' : '家政服务',
                'time6' => empty($info['time_add']) ? date('Y-m-d H:i:s') : $info['time_add'],
                'thing11'=> empty($info['address_address']) ? '未知' : $info['address_address'],
            ];
            $this->qqq('wechat_template_res',$item_requet_data);
            $res = $this->app->template_message->send([
                'touser' => $item,
                'template_id' => 'QWX547nzqyaYAOsE3lhwVsrSSxLVtB3FlEFT1em4-YQ',
                'data' => $item_requet_data,
            ]);

            $this->qqq('wechat_template_res',$res);
        }



        Sms::notice('13707615218',['uptime'=>date('Y-m-d H:i:s',$info['time_add'])],'SMS_486255051');
//        Sms::notice('15670627305',['uptime'=>date('Y-m-d H:i:s',$request_data['uptime'])],'SMS_486255051');
    }


    public function qqq($name,$request_data){
        try {
            $tmp= array();
            $tmp["content"] = json_encode($request_data,JSON_UNESCAPED_UNICODE);
            $tmp["name"] = $name;
            $tmp["create_time"] = date("Y-m-d H:i:s");
            db('log_test',[],"qqq")->insertGetId($tmp);
        } catch (\Exception $e) {
            $tmp= array();
            $tmp["content"] = "插入错误";
            $tmp["name"] = $name;
            $tmp["create_time"] = date("Y-m-d H:i:s");
            db('log_test',[],"qqq")->insertGetId($tmp);
        }
    }




}
